
//--------------------------------------------------------------------
//     此代码由T4模板自动生成
//	   生成时间 2021-09-06 22:10:23 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System;
using TianYu.Core.DataBase;
using TianYu.CMS.Domain.DomainModel;
using TianYu.CMS.Domain.ViewModel.Response;

namespace TianYu.CMS.Service
{
    public class AdvService : BaseRepository<Adv>, IAdvService
    {
        public AdvService(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }

        public async Task<List<Adv>> FindPageListAsync(string keyWords, int pageIndex, int pageSize, RefAsync<int> total)
        {
            var list = await Db.Queryable<Adv>()
                //.WhereIF(!string.IsNullOrEmpty(keyWords), a => a.UserName.Contains(keyWords) || a.TrueName.Contains(keyWords)) 
                .OrderBy(a => a.CreateTime, OrderByType.Desc)
                .ToPageListAsync(pageIndex, pageSize, total);

            return list;
        }

        public async Task<List<AdvQueryResponseModel>> QueryByAdvPos(string[] posCode)
        {
            var list = await Db.Queryable<Adv, AdvPosition>((a, b) => new JoinQueryInfos(JoinType.Left, a.AdvPositionId == b.Id))
                    .Where(a => a.IsDel == false && a.StartTime <= DateTime.Now && a.EndTime > DateTime.Now)
                    .WhereIF(posCode != null && posCode.Length > 0, (a, b) => posCode.Contains(b.Code))
                    .Select((a, b) => new AdvQueryResponseModel()
                    {
                        Id = a.Id,
                        AdvImg = a.AdvImg,
                        AdvName = a.AdvName,
                        JmpUrl = a.JmpUrl, 
                        Code = b.Code
                    })
                    .ToListAsync();

            return list;
        }
    }
}

